package com.wan.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wan.dao.UserMapper;
import com.wan.entity.User;
import com.wan.entity.User;
import com.wan.service.UserService;
import com.wan.utils.DataGridViewResult;
import com.wan.utils.SystemConstant;
import com.wan.vo.UserVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Resource
    private UserService userService;
    /**
     * 员工登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Map<String,Object> map = new HashMap<String, Object>();

        //调用员工登录的方法
        User user = userService.login(username, password);
        //判断对象是否为空，不为空表示登录
        if (user != null){
            //保存当前登录用户
            session.setAttribute(SystemConstant.LOGINUSER,user);
            map.put(SystemConstant.SUCCESS,true);//成功
        }else {
            map.put(SystemConstant.SUCCESS,false);//失败
            map.put(SystemConstant.MESSAGE,"账号密码错误，登录失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 员工列表
     * @param userVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(UserVo userVo){
        //设置分页信息
        PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        //调用查询的方法
        List<User> userList = userService.findUserList(userVo);
        //创建分页对象
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加员工
     * @param user
     * @return
     */

    @RequestMapping(value = "/addUser",produces = "text/html;charset=utf-8")
    public String addUser(User user,HttpSession session){
        Map<String,Object> map = new HashMap<String, Object>();
        //获取当前登录用户
        User loginUser = (User) session.getAttribute(SystemConstant.LOGINUSER);
        //设置创建人
        user.setCreatedBy(loginUser.getId());
        //调用新增员工的方法
        if (userService.addUser(user)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/updateUser",produces = "text/html;charset=utf-8")
    public String updateUser(User user,HttpSession session){
        Map<String,Object> map = new HashMap<String, Object>();
        //设置当前登录用户
        User loginUser = (User) session.getAttribute(SystemConstant.LOGINUSER);
        //设置修改人
        user.setModifyBy(loginUser.getId());
        //调用新增员工的方法
        if (userService.updateUser(user)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/deleteById",produces = "text/html;charset=utf-8")
    public String deleteById(int id){
        Map<String,Object> map = new HashMap<String, Object>();
        //调用删除员工的方法
        if (userService.deleteById(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/resetPwd",produces = "text/html;charset=utf-8")
    public String resetPwd(int id){
        Map<String,Object> map = new HashMap<String, Object>();
        //调用重置密码的方法
        if (userService.resetPwd(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"重置密码成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"重置密码失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/saveUserRole",produces = "text/html;charset=utf-8")
    public String saveUserRole(String roleIds,Integer userId){
        Map<String,Object> map = new HashMap<String, Object>();
        //调用删除员工的方法
        if (userService.saveUserRole(roleIds, userId)){
            map.put(SystemConstant.MESSAGE,"角色分配成功");
        }else {
            map.put(SystemConstant.MESSAGE,"角色分配失败");
        }
        return JSON.toJSONString(map);
    }

}
